#include "calc.h"
#include "ui_calc.h"
#include <QDebug>

Calc::Calc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Calc)
{
    ui->setupUi(this);
    this->setWindowTitle("计算器");
    this->setFixedSize(402, 620);

    // str.mid(start, length);
    // 参数1：截取的起始点
    // 参数2：截取的长度（可选地，如果不设置，则截取到字符串的末尾）
//    QString str = "abcdefd";
//    qDebug() << str.mid(3); // defg
//    qDebug() << str.mid(3, 2); // de

    // 数字按钮
    connect(ui->btn0, &QPushButton::clicked,[this](){this->setNum("0");});
    connect(ui->btn1, &QPushButton::clicked,[this](){this->setNum("1");});
    connect(ui->btn2, &QPushButton::clicked,[this](){this->setNum("2");});
    connect(ui->btn3, &QPushButton::clicked,[this](){this->setNum("3");});
    connect(ui->btn4, &QPushButton::clicked,[this](){this->setNum("4");});
    connect(ui->btn5, &QPushButton::clicked,[this](){this->setNum("5");});
    connect(ui->btn6, &QPushButton::clicked,[this](){this->setNum("6");});
    connect(ui->btn7, &QPushButton::clicked,[this](){this->setNum("7");});
    connect(ui->btn8, &QPushButton::clicked,[this](){this->setNum("8");});
    connect(ui->btn9, &QPushButton::clicked,[this](){this->setNum("9");});
    connect(ui->pointBtn, &QPushButton::clicked,[this](){this->setNum(".");});

    // 运算符
    connect(ui->addBtn, &QPushButton::clicked, [this](){this->setOpt("+");});
    connect(ui->subBtn, &QPushButton::clicked, [this](){this->setOpt("-");});
    connect(ui->mulBtn, &QPushButton::clicked, [this](){this->setOpt("*");});
    connect(ui->divBtn, &QPushButton::clicked, [this](){this->setOpt("÷");});
    connect(ui->perBtn, &QPushButton::clicked, [this](){this->setOpt("%");});

    // 等号
    connect(ui->equalBtn, &QPushButton::clicked, [this](){
        // qDebug() << this->num1 << this->opt << this->num2;
        double result = 0;
        if(this->opt == "+")
        {
            result = this->num1.toDouble() + this->num2.toDouble();
        }
        else if (opt == "-")
        {
            result = this->num1.toDouble() - this->num2.toDouble();
        }
        else if (opt == "*")
        {
            result = this->num1.toDouble() * this->num2.toDouble();
        }
        else if (opt == "÷")
        {
            if(this->num2 == 0)
            {
                ui->lineEdit->setText("除数不能为0");
                return ;
            }
            result = this->num1.toDouble() / this->num2.toDouble();
        }
        else if (opt == "%") {
            result = this->num1.toInt() % this->num2.toInt();
        }
        // QString::number(num) 将num转为字符串类型
        ui->lineEdit->setText(QString::number(result));

        // 为了能够进行连续运算，将结果保存到num1中，并且清空opt、num2
        this->num1 = QString::number(result);
        this->opt = "";
        this->num2 = "";
    });

    // clear
//    connect(ui->clsBtn, &QPushButton::clicked, [this](){
//        this->num1 = "";
//        this->num2 = "";
//        this->opt = "";
//        ui->lineEdit->clear();
//    });

    // back
    connect(ui->backBtn, &QPushButton::clicked,[this](){
        // 对屏幕的数字进行截取
        QString tmp = ui->lineEdit->text();
        if(tmp.size() <= 1 )
        {
            tmp = "";
        }
        else
        {
            tmp = tmp.mid(0, tmp.size() - 1);
            // 如果tmp的最后一位是 . 则继续再截一位
            if(tmp[tmp.size() - 1] == ".")
            {
                tmp = tmp.mid(0, tmp.size() - 1);
            }

        }
        ui->lineEdit->setText(tmp);

        // 根据运算符来区分保存在num1还是num2中
        if(this->opt.isEmpty())
        {
            this->num1 = tmp;
        }
        else
        {
            this->num2 = tmp;
        }
    });

}

void Calc::setNum(QString n)
{
    // 对 . 的特殊检测：1.当屏幕为空时，不能输入 2.当屏幕中已经有 . 不能再输入.
    if((ui->lineEdit->text().isEmpty() && n == "."))
    {
        return ;
    }
    if(ui->lineEdit->text().count(".") == 1 && n == ".")
    {
        return ;
    }

    // 对0的特殊处理：如果屏幕中只有一个0并且现在输入的不是 . 则阻止
    if(ui->lineEdit->text() == "0" && n != ".")
    {
        return ;
    }

    // 判断运算符是否为空
    if(this->opt.isEmpty())
    {
        // true（为空），将数字保存到num1中
        this->num1 += n;
        ui->lineEdit->setText(this->num1);
    }
    else
    {
        // false（不为空，将数字保存到num2）
        this->num2 += n;
        ui->lineEdit->setText(this->num2);
    }


}

void Calc::setOpt(QString o)
{
    this->opt = o;
    ui->lineEdit->clear();
}

Calc::~Calc()
{
    delete ui;
}

void Calc::on_clsBtn_clicked()
{
    this->num1 = "";
    this->num2 = "";
    this->opt = "";
    ui->lineEdit->clear();
}
